package com.link.base.base.product.service;

import com.link.base.base.product.dao.mybatis.mapper.PriceMapper;
import com.link.base.base.product.model.Price;
import com.link.base.base.product.model.Product;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.UserUtil;
import com.link.core.cllog.LCLogger;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author unknown
 * @date unknown
 * @comments 价格操作服务
 */
@Service
public class PriceServiceImpl implements PriceService {

    @Resource
    private PriceMapper priceMapper;

    @Override
    public List<Price> queryPriceListPage(QueryParams qps) throws ServiceException {
        List<Price> list = null;
        try {
            list = priceMapper.queryPriceListPage(qps);
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new ServiceException("PRODUCT-013");
        }
        return list;
    }

    @Override
    public void addPriceInsert(Price record) throws ServiceException {
        try {
            priceMapper.addPriceInsert(record);
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new ServiceException("PRODUCT-014");
        }
    }

    @Override
    public void priceUpdate(Price record) throws ServiceException {
        try {
            priceMapper.priceUpdate(record);
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new ServiceException("PRODUCT-015");
        }
    }

    @Override
    public void delPriceById(Price record) throws ServiceException {
        try {
            priceMapper.delPriceById(record);
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new ServiceException("PRODUCT-016");
        }
    }

    /**
     * 默认根据客户Id查询客户下的所有价格列表
     */
    @Override
    public List<Price> queryPrice(Price record) {
        CoreUser user = UserUtil.getUser();
        record.setAcctId(user.getAcctId());
        record.setLoginCorpId(user.getCorpid());
        return priceMapper.queryPrice(record);
    }

    /**
     * 默认查询客户列表并组成Map
     */
    @Override
    public Map<Long, Price> queryPriceMap(Price record) throws Exception {
        Map<Long, Price> map = new HashMap<Long, Price>();
        //获取企业信息来判断是单价格表还是多价格表
//        EnterpriseInfo enterprise = systemService.getEnterpriseInfo();
//        record.setIsMultiPriceList(enterprise.getIsMultiPriceList());
        List<Price> list = this.queryPrice(record);
        for (Price price : list) {
            Long prodId = price.getProdId();
            if (map.containsKey(prodId)) {
                Price existPrice = map.get(prodId);
                // map中已经包含产品Id了，则取价格小的
                if (existPrice.getPromoPrice() == null) {
                    existPrice.setPromoPrice(0.0);
                }
                if (price.getPromoPrice() == null) {
                    price.setPromoPrice(0.0);
                }
                if (existPrice.getPromoPrice() - price.getPromoPrice() > 0) {
                    map.put(prodId, price);
                }
            } else {
                map.put(prodId, price);
            }
        }
        return map;
    }

    @Override
    public List<Product> reCalculateProductsPrice(List<Product> list) throws Exception {
        List<Product> newProductsList = new ArrayList<Product>();
        Map<Long, Price> map = queryPriceMap(new Price());
        for (Product products : list) {
            Long prodId = products.getId();
            Price price = map.get(prodId);
            if (price != null) {
                LCLogger.info().withMessageKey("reCalculateProductsPrice")
                        .withMessage("产品Id:" + prodId + "的的原始价格为" + products.getPrice() + ",促销价格为"
                                + products.getPromoPrice() + ";价格列表的原始价格为" + price.getPrice()
                                + "促销价格为" + price.getPromoPrice()).flush();
                products.setPrice(price.getPrice());
                products.setPromoPrice(price.getPromoPrice());
                newProductsList.add(products);
            }
        }
        return newProductsList;
    }

}
